package br.ufsc.ctc.ine5622.compilador.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/**
 * This class will launch the information dialog when
 * instantiated.
 * 
 * @author Paulo Pinho
 * @author William di Domenico
 * 
 */

public class DialogSobre extends JDialog implements HyperlinkListener {
	private static final long serialVersionUID = 1L;

	private URL aboutUrl;
	private JEditorPane editorPane = new JEditorPane();

	public DialogSobre(Frame owner) {
		super(owner, "Sobre o compilador");
		composeGui();
	}

	/**
	 * Handles the creation and composition of GUI components.
	 */
	public void composeGui() {
		try {			
			getContentPane().setLayout(new BorderLayout());
		
			this.add(new JLabel(new ImageIcon(new File("").getAbsolutePath()
					+ File.separator + "resources" + File.separator
					+ "icon.png")), BorderLayout.LINE_START);

			composeEditorPane();

			this.pack();
			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			this.setVisible(true);
		} catch (MalformedURLException e) {

		} catch (IOException e) {

		}
	}

	/**
	 * Composes the editor pane, which will load
	 * the help content from a html file. 
	 */
	private void composeEditorPane() throws IOException {
		File f = new File("");
		JScrollPane editorScrollPane = new JScrollPane(editorPane);		
		
		editorPane.addHyperlinkListener(this);
		editorPane.setEditable(false);		
		aboutUrl = new URL("file:" + f.getAbsolutePath() + File.separator
				+ "resources" + File.separator + "sobre.html");	

		editorPane.setPage(aboutUrl);
		
		// Put the editor pane in a scroll pane.		
		editorScrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		editorScrollPane
				.setPreferredSize(new Dimension((int) (Toolkit
						.getDefaultToolkit().getScreenSize().getWidth() / 4),
						(int) (Toolkit.getDefaultToolkit().getScreenSize()
								.getWidth() / 5)));
		editorScrollPane.setMinimumSize(new Dimension(250, 145));
		this.add(editorScrollPane, BorderLayout.CENTER);
	}



	/**
	 * Handles clicks in the hyperlinks, loading 
	 * them in the editor pane.
	 */
	@Override
	public void hyperlinkUpdate(HyperlinkEvent event) {
		if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			try {
				java.awt.Desktop.getDesktop().browse(event.getURL().toURI());
			} catch (IOException e) {
				e.printStackTrace();
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
		}

	}
}
